﻿package  {
	
	import adobe.utils.CustomActions;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.system.fscommand;
	import game.value.GameValues;
	import game.objects.*;
	import game.events.GameEvent;
	import game.math.MyMath;
	import game.objects.num.FiveK;
	import ig.collision.*;
	
	public class main extends MovieClip {
		private var gameTimer:Timer;
		private var timeInterval:uint;
		private var timeRepeat:uint;
		public var FirstPage:String;
		
		public var btn_exit:SimpleButton;
		public var btn_start:SimpleButton;
		//game場景內的東西--------------
		public var gameValue:GameValues;
		public var timeBoard:TimeBoard;
		public var pudding:Pudding;
		public var drink:Drink;
		public var chicken:Chicken;
		public var flower:Flower;
		
		public var rand:Math;
		public var bugType:Number;

		
		var duration:Number = 10;
		var minRand = 20;
		var maxRand = 60;
		//------------------------------
		
		public var overgb:OverBackground;
		
		public function main() {
			super();
			stop();
			
			FirstPage = "game";
			
			this.addFrameScript(0, this.menu, 1, this.about, 2, this.game, 3, this.over);
			gameTimer = new Timer(timeInterval, timeRepeat);
			
			fscommand("fullscreen", "true");
			fscommand("allowscale", "flase");
			
			return;
		}
	
		public function menu():void {
			stop();
			trace("menu");
			
			//測試直接到game
			gotoAndPlay(FirstPage);
			//**按鈕設定
			this.btn_exit.addEventListener(MouseEvent.CLICK, exitGame);
			this.btn_start.addEventListener(MouseEvent.CLICK, startGame);
			
			
			function startGame(event:MouseEvent):void {
				gotoAndPlay("game");
				return;
			}
			
			function exitGame(event:MouseEvent):void {
				fscommand("quit");
				return;
			}

			return;
		}
		//遊戲主要程式
		public function game():void {
			stop();
			
			gameValue = new GameValues();
			this.pudding = new Pudding(this);
			this.drink = new Drink(this);
			this.chicken = new Chicken(this);
			this.timeBoard = new TimeBoard(this);
			
			//隨機產生蟲子
			this.stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
			this.stage.addEventListener(GameEvent.GAME_OVER, GameOver);
			//this.stage.addEventListener(KeyboardEvent.KEY_DOWN, flower.keyboardHandler);
			this.flower = new Flower(this);
			
			return;
		}
		
		public function about():void {
			stop();
			return;
		}
		
		public function over():void {
			stop();
			var endScore:Number = gameValue.getGameScore();
			trace(endScore);
			overgb = new game.objects.OverBackground(gameValue.gameOver, endScore);
			this.stage.addChild(overgb);
			return;
		}
		
		public function enterFrameHandler(e:Event):void {
				if (duration -- < 0)
				{
					generateBugs(randRange(0, 6), randRange(0, 2));
					duration = randRange(minRand,maxRand);
				}
				return;
		}
		
		public function GameOver(e:Event):void {
			trace("GameOver");
			this.stage.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
			
			this.gameValue.showChicken = false;
			this.gameValue.showPudding = false;
			this.gameValue.showDrink = false;
			this.gameValue.showTimeBoard = false;
			this.gameValue.showBugs = false;
			
			this.timeBoard.remove();
			this.gotoAndStop("over");
			
		}
		
		function generateBugs(rand:Number,locate:Number):void {
			switch (rand) 
			{
				case 0:
				case 3:
					var cochroach:Cockroach = new Cockroach(this);
					cochroach.setLocation(locate);
					this.stage.addChild(cochroach);
				break;
				case 1:
				case 2:
					var fly:Fly = new Fly(this);
					fly.setLocation(locate);
					this.stage.addChild(fly);
				break;
				case 4:
				case 5:
				case 6:
					var ant:Ant = new Ant(this);
					ant.setLocation(0);
					this.stage.addChild(ant);
				break;
				default:
			}
			return;
		}
		
		function randRange(min:Number, max:Number):Number { 
			var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min; 
			return randomNum; 
		}
		
	}
}
	
